package com.kaili.basic.controlle;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.CustomerQueryDto;
import com.kaili.basic.pojo.dto.RentAddDto;
import com.kaili.basic.pojo.dto.RentQueryDto;
import com.kaili.basic.pojo.dto.RentUpdateDto;
import com.kaili.basic.pojo.entity.CustomerExport;
import com.kaili.basic.pojo.entity.Rent;
import com.kaili.basic.pojo.entity.RentExport;
import com.kaili.basic.service.RentService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 租金管理 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "租金管理", description = "租金管理相关API")
public class RentController {
    @Autowired
    RentService rentService;

    //新增
    @PostMapping(value = "/rents/addRent")
    @ApiOperation(value = "新增租金",notes = "新增租金")
    public RestStatus addRent(@RequestBody RentAddDto rentAddDto){
        Rent rent = new Rent();
        BeanUtils.copyProperties(rentAddDto, rent);
        Integer count=rentService.querySupplierAndWarehouse(rent);
        if(count>0){
            return CommonStatus.RENT_REPETITION;
        }
        rent.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        rentService.addRent(rent);
        return CommonStatus.ADD_OK;
    }

    //修改
    @PutMapping(value = "/rents/updateRent")
    @ApiOperation(value = "修改租金",notes = "修改租金")
    public RestStatus updateRent(@RequestBody RentUpdateDto rentUpdateDto){
        Rent rent = new Rent();
        BeanUtils.copyProperties(rentUpdateDto, rent);
        Rent rentBy = rentService.queryRentById(rent.getRentId());
        if(rentBy!=null){
            if(rentBy.getSupplierId().longValue()!=rent.getSupplierId().longValue()||
                    rentBy.getWarehouseId().longValue()!=rent.getWarehouseId().longValue()){
                Integer count=rentService.querySupplierAndWarehouse(rent);
                if(count>0){
                    return CommonStatus.RENT_REPETITION;
                }
            }
        }
        rent.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        rentService.updateRent(rent);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/rents/{rentIds}/deleteRents")
    @ApiOperation(value = "删除租金", notes = "删除租金")
    public RestStatus deleteRents(@PathVariable Long[] rentIds) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId().longValue());
        map.put("rentIds",rentIds);
        rentService.deleteRents(map);
        return CommonStatus.DELETE_OK;
    }

    //条件查询获取租金信息
    @GetMapping(value = "/rents/queryRent")
    @ApiOperation(value = "条件查询租金信息",notes = "条件查询租金信息")
    public Page<Rent> queryRent(@ModelAttribute RentQueryDto rentQueryDto) {
        Rent rent = new Rent();
        BeanUtils.copyProperties(rentQueryDto, rent);
        return rentService.queryRent(new Page<Rent>(rent.getPage(),rent.getPageSize()),rent);
    }

    @GetMapping("/rents/export")
    @ApiOperation(value = "导出租金信息")
    public void export(HttpServletResponse response, @ModelAttribute RentQueryDto rentQueryDto) throws RestException {
        List<RentExport> list = rentService.queryRentExport(rentQueryDto);
        ExcelUtil.exportExcel(list,"租金信息","sheet1", RentExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

